package com.flydust.apipulse.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Entity
@Table(name = "endpoints")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Endpoint {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false)
    private String name;

    @Column(nullable = false)
    private String url;

    @Column(nullable = false)
    private String method;

    @Column(name = "check_interval", nullable = false)
    private Integer checkInterval = 60; // 检查间隔（秒）

    @Column(name = "expected_status", nullable = false)
    private Integer expectedStatus = 200; // 期望的HTTP状态码

    @Column(name = "project_id", nullable = false)
    private Long projectId;

    @Column(name = "request_headers")
    private String requestHeaders; // JSON格式的请求头

    @Column(name = "request_body")
    private String requestBody; // 请求体内容

    @Column(name = "params")
    private String params; // 查询参数 (JSON字符串格式)

    @Column(name = "body_type")
    private String bodyType; // 请求体类型 (none, form-data, x-www-form-urlencoded, raw)

    @Column(name = "timeout")
    private Integer timeout = 5000; // 超时时间（毫秒）

    @Column(name = "enabled")
    private Boolean enabled = true; // 是否启用监控

    @Column(name = "directory_id")
    private Long directoryId; // 目录ID

    @Column(name = "created_at")
    private LocalDateTime createdAt;

    @Column(name = "updated_at")
    private LocalDateTime updatedAt;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "project_id", insertable = false, updatable = false)
    @JsonIgnore
    private Project project;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "directory_id", insertable = false, updatable = false)
    @JsonIgnore
    private Directory directory;

    @OneToMany(mappedBy = "endpoint", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JsonIgnore
    private List<HealthCheckResult> healthCheckResults;

    // Constructors
    public Endpoint() {}

    public Endpoint(String name, String url, String method, Long projectId) {
        this.name = name;
        this.url = url;
        this.method = method;
        this.projectId = projectId;
        this.createdAt = LocalDateTime.now();
        this.updatedAt = LocalDateTime.now();
    }

    // Getters and Setters
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public Integer getCheckInterval() {
        return checkInterval;
    }

    public void setCheckInterval(Integer checkInterval) {
        this.checkInterval = checkInterval;
    }

    public Integer getExpectedStatus() {
        return expectedStatus;
    }

    public void setExpectedStatus(Integer expectedStatus) {
        this.expectedStatus = expectedStatus;
    }

    public Long getProjectId() {
        return projectId;
    }

    public void setProjectId(Long projectId) {
        this.projectId = projectId;
    }

    public String getRequestHeaders() {
        return requestHeaders;
    }

    public void setRequestHeaders(String requestHeaders) {
        this.requestHeaders = requestHeaders;
    }

    public String getRequestBody() {
        return requestBody;
    }

    public void setRequestBody(String requestBody) {
        this.requestBody = requestBody;
    }

    // 直接获取params JSON字符串
    public String getParams() {
        return params;
    }

    // 直接设置params JSON字符串
    public void setParams(String params) {
        this.params = params;
    }

    public Integer getTimeout() {
        return timeout;
    }

    public void setTimeout(Integer timeout) {
        this.timeout = timeout;
    }

    public Boolean getEnabled() {
        return enabled;
    }

    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }

    public Long getDirectoryId() {
        return directoryId;
    }

    public void setDirectoryId(Long directoryId) {
        this.directoryId = directoryId;
    }

    public LocalDateTime getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }

    public LocalDateTime getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
    }

    public Project getProject() {
        return project;
    }

    public void setProject(Project project) {
        this.project = project;
    }

    public Directory getDirectory() {
        return directory;
    }

    public void setDirectory(Directory directory) {
        this.directory = directory;
    }

    public List<HealthCheckResult> getHealthCheckResults() {
        return healthCheckResults;
    }

    public void setHealthCheckResults(List<HealthCheckResult> healthCheckResults) {
        this.healthCheckResults = healthCheckResults;
    }

    @PreUpdate
    public void preUpdate() {
        this.updatedAt = LocalDateTime.now();
    }

    @PrePersist
    public void prePersist() {
        this.createdAt = LocalDateTime.now();
        this.updatedAt = LocalDateTime.now();
    }

    public String getBodyType() {
        return bodyType;
    }

    public void setBodyType(String bodyType) {
        this.bodyType = bodyType;
    }
}